/**
 * 
 */
package org.t2.backstage.module.gameserver;

import java.util.List;

import org.gaming.backstage.PageData;
import org.gaming.backstage.PageQuery;
import org.gaming.service.apidoc.annotation.ApiDocument;
import org.gaming.service.asserts.Asserts;
import org.gaming.tool.PageUtil;
import org.gaming.tool.SortUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.t2.backstage.module.TipsCode;
import org.t2.backstage.module.gameserver.from.BulletinForm;
import org.t2.backstage.module.gameserver.model.Bulletin;
import org.t2.backstage.module.gameserver.model.GameChannel;
import org.t2.backstage.module.gameserver.service.BulletinService;
import org.t2.backstage.module.gameserver.service.GameChannelService;

/**
 * @author YY
 *
 */
@Controller
public class BulletinController {

	@Autowired
	private BulletinService bulletinService;
	@Autowired
	private GameChannelService gameChannelService;
	
	@ApiDocument("请求公告数据")
	@RequestMapping(value = "/bulletin/data.auth")
	@ResponseBody
	public PageData<Bulletin> info(PageQuery query) {
		List<Bulletin> list = bulletinService.getEntityList(0);
		SortUtil.sort(list, Bulletin::getId);
		
		PageData<Bulletin> pageData = new PageData<>();
		pageData.setCount(list.size());
		pageData.setData(PageUtil.pageN(list, query.getPage(), query.getLimit()));
		return pageData;
	}
	
	@ApiDocument("保存公告数据，数据ID小于等于0时插入数据，大于0时更新对应数据")
	@RequestMapping(value = "/bulletin/save.authw")
	@ResponseBody
	public Bulletin save(BulletinForm form) {
		Bulletin bulletin = null;
		if(form.getId() <= 0) {
			bulletin = new Bulletin();
			bulletin.setTitle(form.getTitle());
			bulletin.setContent(form.getContent());
			bulletinService.insert(bulletin);
		} else {
			bulletin = bulletinService.getEntity(0, form.getId());
			Asserts.isTrue(bulletin != null, TipsCode.BULLETIN_MISS, form.getId());
			
			bulletin.setTitle(form.getTitle());
			bulletin.setContent(form.getContent());
			bulletinService.update(bulletin);
		}
		return bulletin;
	}
	
	@ApiDocument("删除渠道数据，并删除与之关联的服务器关系数据")
	@RequestMapping(value = "/bulletin/delete.authw")
	@ResponseBody
	public void delete(@RequestParam("id") long id) {
		Bulletin bulletin = bulletinService.getEntity(0, id);
		Asserts.isTrue(bulletin != null, TipsCode.BULLETIN_MISS, id);
		
		for(GameChannel gameChannel : gameChannelService.getEntityList(0)) {
			if(gameChannel.getBulletinId() == id) {
				Asserts.isTrue(false, TipsCode.BULLETIN_USING_IN_CHANNEL, gameChannel.getName());
			}
		}
		bulletinService.delete(bulletin);
	}
}
